package com.clx.rmp.mx.core;

import com.alibaba.fastjson.JSON;
import com.clx.rmp.common.constant.DirConstant;
import com.clx.rmp.common.utils.FileUtil;
import org.dmg.pmml.FieldName;
import org.dmg.pmml.PMML;
import org.jpmml.evaluator.*;
import org.jpmml.model.PMMLUtil;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ExePmml {
    public String test() {
        System.out.println("文件加密被执行了");
        return "fdafdas";
    }

    static String str = "{\"hzdbactivecrdtnum\":0,\"hzdbactnumsum\":0,\"hzdbamtsum\":0,\"hzdbbadbalsum\":0,\"hzdbbalamtrate\":0,\"hzdbbalsum\":0,\"hzdbbankloanamtrate\":0,\"hzdbcarloanamtsum\":0,\"hzdbclactualrepaymentsum\":0,\"hzdbclfirstopendatedif\":0,\"hzdbcllastopendatedif\":0,\"hzdbclovdueamtmax\":0,\"hzdbclovduemax\":0,\"hzdbclovduenum12m\":0,\"hzdbclovduenum24m\":0,\"hzdbclquerydays\":0,\"hzdbclquerynum12m\":0,\"hzdbclquerynum1m\":0,\"hzdbclquerynum3m\":0,\"hzdbclquerynum6m\":0,\"hzdbclqueryorgnum12m\":0,\"hzdbclqueryorgnum3m\":0,\"hzdbclqueryorgnum6m\":0,\"hzdbclrepaymentsum\":0,\"hzdbconsumeloanamtrate\":0,\"hzdbcrdtamtavg\":0,\"hzdbcrdtamtmax\":0,\"hzdbcrdtamtuserate\":0,\"hzdbcrdtavgamtuserate\":0,\"hzdbcrdtrepaymentrate\":0,\"hzdbcrdtsigleorgamtmax\":0,\"hzdbdiploma\":10,\"hzdbhousecarrepaymentsum\":0,\"hzdbhouseloanamtsum\":0,\"hzdbisbadcl\":0,\"hzdblastcrdtamt\":0,\"hzdbloanactnum\":0,\"hzdbloanamtavg\":0,\"hzdbloanamtlast\":0,\"hzdbloanamtmax\":0,\"hzdbloanamtsum\":0,\"hzdbloanbaluserate\":0,\"hzdbloandeadlineavg\":0,\"hzdbloandeadlinemax\":0,\"hzdbmarrigestu\":10,\"hzdborgnumsum\":0,\"hzdbovduenummax12m\":0,\"hzdbovduenummax24m\":0,\"hzdbquerynum12m\":0,\"hzdbquerynum1m\":0,\"hzdbquerynum3m\":0,\"hzdbquerynum6m\":0,\"hzdbqueryselfnum1m\":0,\"hzdbrepaymentavgamt6m\":0,\"hzdbsmallloanamtrate\":0,\"hzdbworkyearmax\":0}\n";

    public static void main(String[] args) {
        ExePmml exePmml = new ExePmml();
        Map map = JSON.parseObject(str, Map.class);
        Double aDouble = null;
//        aDouble = exePmml.predictLrHeart(JSON.parseObject(str, Map.class));

        aDouble = exePmml.main11(map);
        System.out.println("执行结果=" + aDouble);
    }

    public Double predictLrHeart(Map<String, Double> irismap) {
        ByteArrayInputStream bis = null;
        try {
            //1.读取pmml文件
            String pmmlFile = DirConstant.CONF_HOME_DIR + File.separator + DirConstant.PMML_NAME;
            byte[] bytes = FileUtil.readByte(new File(pmmlFile));
            System.out.println(new String(bytes));
//            bytes = EncryptUtil.decrypt(bytes);
//            bis = new ByteArrayInputStream(bytes);
            FileInputStream bis1 = new FileInputStream(new File(pmmlFile));
            PMML pmml = PMMLUtil.unmarshal(bis1);


            //2.执行模型文件
            ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
//            ModelEvaluator<?> modelEvaluator = modelEvaluatorFactory.newModelEvaluator(pmml);
//            Evaluator evaluator = (Evaluator) modelEvaluator;

            Evaluator evaluator = null;

            List<InputField> inputFields = evaluator.getInputFields();
            Map<FieldName, FieldValue> argements = new LinkedHashMap<>();
            for (InputField inputField : inputFields) {
                FieldName inputFieldName = inputField.getName();
                Object raeValue = irismap.get(inputFieldName.getValue());
                FieldValue inputFieldValue = inputField.prepare(raeValue);
                argements.put(inputFieldName, inputFieldValue);
            }
            Map<FieldName, ?> results = evaluator.evaluate(argements);
            List<TargetField> targetFields = evaluator.getTargetFields();
            for (TargetField targetField : targetFields) {
                FieldName targetFieldName = targetField.getName();
                Object targetFieldValue = results.get(targetFieldName);
//                System.out.println("target: " + targetFieldName.getValue());
                System.out.println(targetFieldValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static Double main11(Map<String, Double> irismap) {


//        List<InputField> inputFields = evaluator.getInputFields();
//        Map<FieldName, FieldValue> arguments = new LinkedHashMap<FieldName, FieldValue>();
//        for (InputField inputField : inputFields) {
//            FieldName inputFieldName = inputField.getName();
//            //Object rawValue =Arrays.asList(1,2,3,4);
//            Object rawValue = 1;
//            System.out.println(inputFieldName.getValue() + ":" + rawValue);
//            FieldValue inputFieldValue = inputField.prepare(rawValue);
//            arguments.put(inputFieldName, inputFieldValue);
//        }
//
//        Map<FieldName, ?> results = evaluator.evaluate(arguments);
//        List<TargetField> targetFields = evaluator.getTargetFields();
//
//        TargetField targetField = targetFields.get(0);
//        FieldName targetFieldName = targetField.getName();
//
//        Object targetFieldValue = results.get(targetFieldName);
//        System.out.println("target: " + targetFieldName.getValue() + " value: " + targetFieldValue);


        ByteArrayInputStream bis = null;
        try {
            String pmmlFile1 = DirConstant.CONF_HOME_DIR + File.separator + DirConstant.PMML_NAME;
            File pmmlFile = new File(pmmlFile1);
            Evaluator evaluator = new LoadingModelEvaluatorBuilder().load(pmmlFile).build();

            List<InputField> inputFields = evaluator.getInputFields();
            Map<FieldName, FieldValue> argements = new LinkedHashMap<>();
            for (InputField inputField : inputFields) {
                FieldName inputFieldName = inputField.getName();
                Object raeValue = irismap.get(inputFieldName.getValue());
                FieldValue inputFieldValue = inputField.prepare(raeValue);
                argements.put(inputFieldName, inputFieldValue);
            }
            Map<FieldName, ?> results = evaluator.evaluate(argements);
            List<TargetField> targetFields = evaluator.getTargetFields();
            for (TargetField targetField : targetFields) {
                FieldName targetFieldName = targetField.getName();
                Object targetFieldValue = results.get(targetFieldName);
//                System.out.println("target: " + targetFieldName.getValue());
                System.out.println(targetFieldValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
